// @flow
import React, { Component } from 'react'
import { Form, Select, Input } from 'antd'

const FormItem = Form.Item
const Option = Select.Option
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 }
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 14 }
  }
}
type Props = {
  pointData: Array<Object>,
  variableType: Array<Object>,
  form: Object,
  type?: string
}

class VarModalContent extends Component {
  props: Props
  // constructor (props: Props) {
  //   super(props)
  // }
  checkNum (rule, value, callback) {
    const checkReg = /^[a-zA-Z0-9_]*$/
    if (value && !checkReg.test(value)) {
      callback('只能输入英文、数字和下划线')
    } else {
      callback()
    }
  }
  render () {
    const { getFieldDecorator } = this.props.form
    const { variableType, type } = this.props
    return (
      <Form>
        {
          type !== 'single'
          ? <FormItem {...formItemLayout} label='测点'>
            {getFieldDecorator('monitorPointId', {
              rules: [
                { required: true, message: '请选择测点' }
              ]
            })(
              <Select showSearch
                filterOption={(input, option) => {
                  return option.props.children.indexOf(input) >= 0
                }} placeholder='请选择测点'>
                {
                  this.props.pointData.map((d, i) => {
                    return <Option key={d.id} value={`${d.id}`}>{d.sensorName}</Option>
                  })
                }
              </Select>
            )}
          </FormItem>
          : false
        }

        <FormItem {...formItemLayout} label='KMX 名字' hasFeedback>
          {getFieldDecorator('kmxVariableName', {
            rules: [
              { required: true, message: '请填写KMX 名字', whitespace: true }
            ]
          })(
            <Input placeholder='请填写KMX 名字' />
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='KMX 名字en' hasFeedback>
          {getFieldDecorator('kmxVariableNameEn', {
            rules: [
              { required: true, message: '请填写KMX 名字en', whitespace: true },
              { validator: this.checkNum }
            ]
          })(
            <Input placeholder='请填写KMX 名字en' />
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='KMX 类型'>
          {getFieldDecorator('kmxVariableType', {
            rules: [
              { required: true, message: '请选择KMX 类型', whitespace: true }
            ]
          })(
            <Select showSearch
              filterOption={(input, option) => {
                return option.props.children.indexOf(input) >= 0
              }} placeholder='请选择KMX 类型'>
              {
                variableType.map((v, i) => {
                  return <Option key={i} value={v.value}>{v.label}</Option>
                })
              }
            </Select>
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='KMX 单位' hasFeedback>
          {getFieldDecorator('kmxUnit', {})(
            <Input placeholder='请填写KMX 单位' />
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='标准值' hasFeedback>
          {getFieldDecorator('standardValue', {})(
            <Input placeholder='请填写标准值' />
          )}
        </FormItem>
        <FormItem {...formItemLayout} label='更新频率' hasFeedback>
          {getFieldDecorator('frequency', {})(
            <Input placeholder='请填写更新频率' />
          )}
        </FormItem>
      </Form>
    )
  }
}

export default Form.create()(VarModalContent)
